﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Baccarat.CardGameFramework;

namespace Baccarat
{

    public class BaccaratGame
    {

        #region Fields

        // private Deck and Player objects for the current deck, dealer, and player
        private Deck[] deck = null;
        private Player dealer = null;
        private Player[] autoPlayer = null;
        private int numOfAutoPlayers = 0;
        private Player userPlayer = null;
        private Player currentPlayer = null;
        private Deck currentDeck = null;
        private int numOfDecks = 6;

        #endregion

        #region Properties

        // public properties to return the current player, dealer, and current deck
        public Player CurrentPlayer { get { return currentPlayer; } }
        public Player Dealer { get { return dealer; } }
        public Deck CurrentDeck { get { return currentDeck; } }

        #endregion

        #region Constructor
        /// <summary>
        /// Main Constructor for Baccarat Game
        /// </summary>
        /// <param name="initBalance"></param>
        public BaccaratGame(int initBalance)
        {
            // Create a dealer and one player with the initial balance.
            dealer = new Player();
            userPlayer = new Player(initBalance);
            currentPlayer = userPlayer;
        }

        #endregion Constructor


        #region Game Methods

        /// <summary>
        /// Deals a new game.  This is invoked through the Deal button in BlackJackForm.cs
        /// </summary>
        public void DealNewGame()
        {
            // Create a new deck and then shuffle the deck
            deck = new Deck[numOfDecks];  //as per Chemin de Fer Baccarat
            
            //Shuffle all decks:
            for (int i = 0; i < numOfDecks; i++)
            {
                deck[i] = new Deck();
                deck[i].Shuffle();
            }

            int indexOfDeck = 0;

            // Reset the player and the dealer's hands in case this is not the first game
            userPlayer.NewHand();
            dealer.NewHand();
            if (numOfAutoPlayers > 0)
            {
                autoPlayer = new Player[numOfAutoPlayers];
                foreach (Player player in autoPlayer)
                {
                    player.NewHand();
                }
                GameStatistics.AutoPlayerStats = new Statistics[numOfAutoPlayers];
            }


            // Deal 2 cards to each person's hand
            Card c = null;

            for (int i = 0; i < 2; i++)
            {
                if (numOfAutoPlayers > 0)
                {
                    autoPlayer = new Player[numOfAutoPlayers];
                    foreach (Player player in autoPlayer)
                    {
                        if (deck[indexOfDeck].CardsInDeck == 0)
                        {
                            if (indexOfDeck < numOfDecks)
                            {
                                indexOfDeck++;
                            }
                        }

                        c = deck[indexOfDeck].Draw();
                        c.IsCardUp = false;
                        player.Hand.Cards.Add(c);


                    }
                }

                c = deck[indexOfDeck].Draw();
                c.IsCardUp = true;
                userPlayer.Hand.Cards.Add(c);

                c = deck[indexOfDeck].Draw();
                c.IsCardUp = false;
                dealer.Hand.Cards.Add(c);


            }


            // Give the player and the dealer a handle to the current deck
            userPlayer.CurrentDeck = deck[indexOfDeck];
            dealer.CurrentDeck = deck[indexOfDeck];
        }

        /// <summary>
        /// This method finishes playing the dealer's hand
        /// </summary>
        public void DealerPlay()
        {
            // Dealer's card that was facing down is turned up when they start playing
            dealer.Hand.Cards[1].IsCardUp = true;

            // Check to see if dealer has a hand that is less than 17
            // If so, dealer should keep hitting until their hand is greater or equal to 17
            if (dealer.Hand.GetSumOfHand() < 17)
            {
                dealer.Draw();
                DealerPlay();
            }
        }

        /// <summary>
        /// Update the player's record with a loss
        /// </summary>
        public void PlayerLose()
        {
            GameStatistics.PlayerStats.Losses++;
        }

        /// <summary>
        /// Update the player's record with a win
        /// </summary>
        public void PlayerWin()
        {
            GameStatistics.PlayerStats.Wins++;
        }
        #endregion



    }
}
